package bb.lanxing.lib.devices.sprint.sync;

import android.content.Context;
import android.text.TextUtils;
import bb.lanxing.lib.devices.core.sync.AbstractSyncManager;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.sprint.entity.SprintMaps;
import bb.lanxing.lib.devices.sprint.model.SprintMapModel;
import bb.lanxing.lib.devices.sprint.model.impl.ServerSprintMapsModel;
import bb.lanxing.lib.devices.sprint.model.impl.SprintDeviceMapModel;
import bb.lanxing.lib.devices.sprint.model.impl.StaticSprintMapsModel;
import bb.lanxing.lib.devices.sprint.utils.DeviceControllerHelper;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.schedulers.Schedulers;

public class SprintMapManager extends AbstractSyncManager implements SprintMapModel.Callback {
    private String address;
    protected XossDeviceController controller;
    private Subscription requestMapsSub;
    protected SprintMapModel serverMapModel;
    protected SprintMapModel sprintMapModel;
    protected SprintMapModel staticMapModel;

    @Override
    public void abortAll() {
    }

    @Override
    public boolean isSynchronising() {
        return false;
    }

    @Override
    public boolean isSynchronisingWithMultiFiles() {
        return false;
    }

    @Override
    protected boolean onSync(DeviceFile deviceFile) {
        return false;
    }

    public SprintMapManager(Context context, String str) {
        this.controller = DeviceControllerHelper.getXossDeviceController(str);
        this.address = str;
        this.sprintMapModel = createSprintDeviceMapsModel(context, str);
        this.staticMapModel = createStaticSprintMapsModel(context);
        this.serverMapModel = createServerSprintMapsModel(context);
        this.sprintMapModel.setCallback(this);
        this.serverMapModel.setCallback(this);
        this.staticMapModel.setCallback(this);
    }

    protected SprintMapModel createServerSprintMapsModel(Context context) {
        return new ServerSprintMapsModel(context);
    }

    protected SprintMapModel createStaticSprintMapsModel(Context context) {
        return new StaticSprintMapsModel(context);
    }

    protected SprintMapModel createSprintDeviceMapsModel(Context context, String str) {
        return new SprintDeviceMapModel(context, str);
    }

    @Override
    public void readFileList() {
        if (isSynchronising()) {
            notifyDeviceFiles(getDeviceFiles());
            return;
        }
        Subscription subscription = this.requestMapsSub;
        if (subscription != null && !subscription.isUnsubscribed()) {
            this.requestMapsSub.unsubscribe();
        }
        this.requestMapsSub = loadSprintMapsFromModels().subscribe((Subscriber<? super List<SprintMaps>>) new Subscriber<List<SprintMaps>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
                if (isUnsubscribed()) {
                    return;
                }
                SprintMapManager.this.notifyMaps(null);
            }

            @Override
            public void onNext(List<SprintMaps> list) {
                if (isUnsubscribed()) {
                    return;
                }
                SprintMapManager.this.notifyMaps(list);
            }
        });
    }

    @Override
    public boolean isSynchronised(long j) {
        return this.sprintMapModel.isSynced(j);
    }

    @Override
    public void abort() {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.abort();
        }
    }

    @Override
    public boolean sync(String str) {
        throw new UnsupportedOperationException("No impl");
    }

    @Override
    public boolean resync(DeviceFile deviceFile) {
        throw new UnsupportedOperationException("No impl");
    }

    @Override
    public String getPath(long j) {
        return this.staticMapModel.getPath(j);
    }

    @Override
    public void onProgressUpdate(long j, float f) {
        notifyProgressUpdate(getDeviceFileById(j), f);
    }

    @Override
    public void onMapStatus(long j, int i) {
        notifySyncStatus(getDeviceFileById(j), i);
    }

    protected Observable<List<SprintMaps>> loadSprintMapsFromModels() {
        return Observable.create(new Observable.OnSubscribe<List<SprintMaps>>() {
            @Override
            public void call(Subscriber<? super List<SprintMaps>> subscriber) {
                try {
                    SprintMapManager.this.staticMapModel.loadSprintMaps();
                } catch (Exception unused) {
                }
                try {
                    List<SprintMaps> loadSprintMaps = SprintMapManager.this.sprintMapModel.loadSprintMaps();
                    if (subscriber.isUnsubscribed()) {
                        return;
                    }
                    subscriber.onNext(loadSprintMaps);
                    subscriber.onCompleted();
                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        }).subscribeOn(Schedulers.io());
    }

    @Override
    public void delete(long j) {
        this.sprintMapModel.deleteById(j);
    }

    @Override
    public void release() {
        super.release();
        SprintMapModel sprintMapModel = this.staticMapModel;
        if (sprintMapModel != null) {
            sprintMapModel.release();
        }
        SprintMapModel sprintMapModel2 = this.serverMapModel;
        if (sprintMapModel2 != null) {
            sprintMapModel2.release();
        }
        SprintMapModel sprintMapModel3 = this.sprintMapModel;
        if (sprintMapModel3 != null) {
            sprintMapModel3.release();
        }
        this.staticMapModel = null;
        this.serverMapModel = null;
        this.sprintMapModel = null;
    }

    protected void notifyMaps(List<SprintMaps> list) {
        if (list != null) {
            for (SprintMaps sprintMaps : list) {
                SprintMaps sprintMapById = this.serverMapModel.getSprintMapById(sprintMaps.getId());
                if (sprintMapById != null) {
                    sprintMaps.setName(sprintMapById.getName());
                    sprintMaps.setNameEn(sprintMapById.getNameEn());
                } else {
                    SprintMaps sprintMapById2 = this.staticMapModel.getSprintMapById(sprintMaps.getId());
                    if (sprintMapById2 != null) {
                        sprintMaps.setName(sprintMapById2.getName());
                        sprintMaps.setNameEn(sprintMapById2.getNameEn());
                    }
                }
            }
            List<SprintMaps> expandMaps = expandMaps(list);
            ArrayList arrayList = new ArrayList(list);
            setDeviceFiles(arrayList);
            initSprintMapsStatus(expandMaps);
            notifyDeviceFiles(arrayList);
            return;
        }
        notifyDeviceFiles(null);
    }

    @Override
    public DeviceFile getDeviceFileById(long j) {
        SprintMaps sprintMapById = this.serverMapModel.getSprintMapById(j);
        if (sprintMapById == null) {
            sprintMapById = this.sprintMapModel.getSprintMapById(j);
        }
        if (sprintMapById == null) {
            sprintMapById = this.staticMapModel.getSprintMapById(j);
        }
        return sprintMapById == null ? super.getDeviceFileById(j) : sprintMapById;
    }

    @Override
    public DeviceFile getDeviceFileByName(String str) {
        SprintMaps sprintMapByName = this.sprintMapModel.getSprintMapByName(str);
        if (sprintMapByName == null) {
            sprintMapByName = this.serverMapModel.getSprintMapByName(str);
        }
        if (sprintMapByName == null) {
            sprintMapByName = this.staticMapModel.getSprintMapByName(str);
        }
        return sprintMapByName == null ? super.getDeviceFileByName(str) : sprintMapByName;
    }

    private List<SprintMaps> expandMaps(List<SprintMaps> list) {
        if (list != null) {
            ArrayList arrayList = new ArrayList();
            for (SprintMaps sprintMaps : list) {
                List<SprintMaps> availableMaps = getAvailableMaps(Integer.MAX_VALUE, sprintMaps);
                if (availableMaps != null) {
                    arrayList.addAll(availableMaps);
                }
            }
            return arrayList;
        }
        return null;
    }

    protected void updateMapsStatus(SprintMaps sprintMaps) {
        long id = sprintMaps.getId();
        getDeviceFileById(id);
        if (this.serverMapModel.isDownloaded(id)) {
            if (this.sprintMapModel.isSynced(id)) {
                updateStatus(id, DeviceFileStatus.STATUS_SYNCED.getStatus());
            } else {
                updateStatus(id, DeviceFileStatus.STATUS_DOWNLOADED.getStatus());
            }
        } else if (this.serverMapModel.isDownloading(id)) {
            updateStatus(id, DeviceFileStatus.STATUS_DOWNLOADING.getStatus());
        } else if (this.sprintMapModel.isSynced(id)) {
            updateStatus(id, DeviceFileStatus.STATUS_SYNCED.getStatus());
        } else {
            DeviceFileStatus deviceFileStatus = DeviceFileStatus.STATUS_NONE;
            SprintMaps sprintMapById = this.serverMapModel.getSprintMapById(id);
            SprintMaps sprintMapById2 = this.serverMapModel.getSprintMapById(id);
            if (sprintMapById2 != null && sprintMapById != null && sprintMapById2.getVersion() < sprintMapById.getVersion()) {
                deviceFileStatus = DeviceFileStatus.STATUS_NEED_UPGRADE;
            }
            updateStatus(id, deviceFileStatus.getStatus());
        }
    }

    private void initSprintMapsStatus(List<SprintMaps> list) {
        for (SprintMaps sprintMaps : list) {
            updateMapsStatus(sprintMaps);
        }
    }

    public List<SprintMaps> getAvailableMaps(int i, SprintMaps sprintMaps) {
        if (sprintMaps == null || i <= 0) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        List<SprintMaps> maps = sprintMaps.getMaps();
        if (!TextUtils.isEmpty(sprintMaps.getUrl())) {
            arrayList.add(new SprintMaps(sprintMaps.getName(), sprintMaps.getNameEn(), sprintMaps.getId(), sprintMaps.getSize(), sprintMaps.getVersion()));
        }
        if (maps != null) {
            for (SprintMaps sprintMaps2 : maps) {
                i--;
                List<SprintMaps> availableMaps = getAvailableMaps(i, sprintMaps2);
                if (availableMaps != null) {
                    arrayList.addAll(availableMaps);
                }
            }
        }
        return arrayList;
    }
}
